package com.woniu.communitymanagement.service.impl;

import com.woniu.communitymanagement.entity.HttpResult;
import com.woniu.communitymanagement.entity.Records;
import com.woniu.communitymanagement.mapper.RecordsMapper;
import com.woniu.communitymanagement.service.IRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

@Service
public class RecordsServiceImpl implements IRecordsService {

    @Autowired(required = false)
    private RecordsMapper recordsMapper;

    @Override
    public HttpResult getList(Integer typeId, String houseNumber, Integer pageIndex, Integer pageSize) {
        List<Records> records = recordsMapper.selectAll(typeId, houseNumber, (pageIndex - 1) * pageSize, pageSize);
        int count = recordsMapper.count(typeId, houseNumber);
        HttpResult result = null;
        if (records!=null && records.size()>0){//获取到数据
            result = new HttpResult(records,count,200,null);
        }else {
            result = new HttpResult(null,0,500,"没有查询到更多数据");
        }
         return result;
    }

    @Override
    public HttpResult addRecords(Records records) {
        int count = recordsMapper.insertRecords(records);
        HttpResult result = null;
        if (count>0){//成功
            result = new HttpResult(null,0,200,"添加成功");
        }else {//失败
            result = new HttpResult(null,0,500,"添加失败");
        }
        return result;
    }

    @Override
    public HttpResult updateRecords(Records records) {
        int count = recordsMapper.updateRecords(records);
        HttpResult result = null;
        if (count>0){//成功
            result = new HttpResult(null,0,200,"修改失败");
        }else {//失败
            result = new HttpResult(null,0,500,"修改失败");
        }
        return result;
    }

    @Override
    public HttpResult removeRecords(Integer id) {
        int count = recordsMapper.deleteRecords(id);
        HttpResult result = null;
        if (count>0){//成功
            result = new HttpResult(null,0,200,"删除失败");
        }else {//失败
            result = new HttpResult(null,0,500,"删除失败");
        }
        return result;
    }

    @Override
    public HttpResult getInfo(Integer id) {
        Records record = recordsMapper.selectById(id);
        HttpResult result = null;
        if (record!=null){//成功
            result = new HttpResult(record,0,200,null);
        }else {//失败
            result = new HttpResult(null,0,500,"没有查询到更多数据");
        }
        return result;
    }


}
